# Title     : TODO
# Objective : TODO
# Created by: xueyj
# Created on: 2020/10/10
pacman::p_load(lazyopt, ggplot2, dplyr, magrittr, stringr)

lm_eqn <- function(df) {
  m <- lm(y ~ x, df)
  eq <- substitute(italic(y) == a + b %.% italic(x) * "," ~ ~italic(r)^2 ~ "=" ~ r2,
                   list(a = format(coef(m)[1], digits = 2),
                        b = format(coef(m)[2], digits = 2),
                        r2 = format(summary(m)$r.squared, digits = 3)))
  as.character(as.expression(eq))
}

arg <- c("-i", "E:/projects/Lysimeter_Systems/src/output2/A13-l.xls",
         "-o", "E:/projects/Lysimeter_Systems/src/image", "-if", "png")

opt <- matrix(c(
  "inputfile", "i", 2, "character", "Set the input file path ", " ",
  "outputpackage", "o", 2, "character", "Set the output folder path", " ",
  "imageformat", "if", 1, "character", "set image type", "pdf",
  "choose_col", "cc", 1, "character", "set y axis ,According to the table header", "vpd",
  "plot_type", "pt", 1, "character", "onlt point or zhexian", "point",
  "resolution", "dpi", 1, "numeric", "Set the resolution to allow 72,96,300 or 600", "300"
), byrow = TRUE, ncol = 6) %>% lazyopt(arg=NULL)

resolution <- match.arg(opt$resolution %>%
                          as.character(), c("72", "96", "300", "600")) %>%
  as.numeric()

path <- opt$inputfile

title <- (path %>% basename() %>% str_split("-"))[[1]][1]

data <- read.delim(file = path, check.names = FALSE)

drawdata <- data %>%
  select(c("ACCE", "ACCP")) %>%
  set_colnames(c("x", "y"))
drawdata <- drawdata[1:(nrow(drawdata) - 1),]

p <- ggplot(data, aes(x = ACCE, y = ACCP)) +
  geom_point() +
  geom_smooth(method = "lm")
p <- p +
  geom_text(x = colMeans(drawdata)[1] * 0.5, y = colMeans(drawdata)[2] * 1.5,
            label = lm_eqn(drawdata), parse = T) +
  theme_bw() +
  theme(
    panel.border = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    legend.background = element_blank()
  ) +
  labs(title = title)
ggsave(p, filename = opt %$%
  paste0(outputpackage, "/", title, "-ACCP-ACCE.", opt$imageformat), dpi = resolution
)
k <- data %>% names()

#for (i in 3:(k %>% length())) {
data <- data[, -1]

z1 <- opt %$% match.arg(choose_col, colnames(data)[3:(k %>% length())]) %>%
  as.character()

z <- data[, which(colnames(data) == z1)]

#z <- data[, i]

if (opt$plot_type == "zhexian") {
  (ggplot(data = data) +
    geom_line(aes(x = Time, y = z, group = 1)) +
    theme_bw() +
    theme(
      panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      legend.background = element_blank(),
      axis.text.x = element_text(angle = 270)
    ) +
    labs(title = title, x = "Time", y = z1)) %>% ggsave(
    filename = opt %$%
      paste0(outputpackage, "/", title, "-Time-", z1, ".", opt$imageformat), dpi = resolution)
  #}
}

if (opt$plot_type == "point") {
  (ggplot(data = data) +
    geom_point(aes(x = Time, y = z), color = "blue") +
    theme_bw() +
    theme(
      panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      legend.background = element_blank(),
      axis.text.x = element_text(angle = 270)
    ) +
    labs(title = title, x = "Time", y = z1)) %>% ggsave(
    filename = opt %$%
      paste0(outputpackage, "/", title, "-Time-", z1, ".", opt$imageformat), dpi = resolution)
  #}
}


